package com.cd2cd.code_template.service.impl;

import com.cd2cd.code_template.code_template.domain.SysRelationAuth;
import com.cd2cd.code_template.code_template.domain.SysRole;
import com.cd2cd.code_template.code_template.domain.gen.SysRelationAuthCriteria;
import com.cd2cd.code_template.code_template.domain.gen.SysRoleCriteria;
import com.cd2cd.code_template.code_template.mapper.SysAuthMapper;
import com.cd2cd.code_template.code_template.mapper.SysRelationAuthMapper;
import com.cd2cd.code_template.code_template.mapper.SysRoleMapper;
import com.cd2cd.code_template.configuration.constants.ServiceCode;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService;
import com.cd2cd.code_template.service.SysRoleService;
import com.cd2cd.code_template.util.BeanUtil;
import com.cd2cd.code_template.vo.BaseRes;
import com.cd2cd.code_template.vo.PageData;
import com.cd2cd.code_template.vo.SysRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class SysRoleServiceImpl implements SysRoleService {


    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysAuthMapper sysAuthMapper;

    @Resource
    private SysRelationAuthMapper sysRelationAuthMapper;

    @Override
    public BaseRes<PageData<SysRoleVo>> getRoleList(SysRoleVo sysRoleVo) {

        Integer current = sysRoleVo.getCurrent() == null ? 1 : sysRoleVo.getCurrent();
        Integer pageSize = sysRoleVo.getPageSize() == null ? 10 : sysRoleVo.getPageSize();

        BaseRes<PageData<SysRoleVo>> res = new BaseRes<>(new PageData<>(current, pageSize),ServiceCode.SUCCESS);

        Integer mysqlLength = pageSize;
        Integer mysqlOffset = (current - 1) * mysqlLength;

        SysRoleCriteria criteria = new SysRoleCriteria();

        criteria.setMysqlLength(mysqlLength);
        criteria.setMysqlOffset(mysqlOffset);
        criteria.setOrderByClause(" id desc ");

        SysRoleCriteria.Criteria criteriaCriteria = criteria.createCriteria();

        String name = sysRoleVo.getName();

        if (StringUtils.isNotBlank(name)) {
            criteriaCriteria.andNameLike("%" + name + "%");
        }

        String status = sysRoleVo.getStatus();

        if (StringUtils.isNotBlank(status)) {
            criteriaCriteria.andStatusEqualTo(status);
        }

        long count = sysRoleMapper.countByExample(criteria);

        if (count > 0) {
            List<SysRole> sysRoles = sysRoleMapper.selectByExample(criteria);
            List<SysRoleVo> sysRoleVos = BeanUtil.voConvertList(sysRoles, SysRoleVo.class);
            res.getData().setRows(sysRoleVos);
        }
        return res;
    }



    @Override
    public BaseRes<String> addRole(SysRoleVo sysRoleVo) {
        sysRoleVo.setUpdateTime(new Date());
        sysRoleVo.setCreateTime(new Date());
        sysRoleMapper.insertSelective(sysRoleVo);

        saveAuthConfig(sysRoleVo.getId(),sysRoleVo.getAuths());

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> updateRole(SysRoleVo sysRoleVo) {
        sysRoleVo.setUpdateTime(new Date());

        saveAuthConfig(sysRoleVo.getId(),sysRoleVo.getAuths());
        sysRoleMapper.updateByPrimaryKeySelective(sysRoleVo);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }


    private void saveAuthConfig(Integer typeId,List<String> dataGuids) {

        SysRelationAuthCriteria criteria = new SysRelationAuthCriteria();
        criteria.createCriteria().andTypeEqualTo("role").andRelationIdEqualTo(typeId).andLoginTypeEqualTo(LoadUserService.Backend.class.getSimpleName());

        sysRelationAuthMapper.deleteByExample(criteria);

        if (null == dataGuids) {
            return;
        }

        // 插入
        for (String guid : dataGuids) {
            SysRelationAuth sysRelationAuth = new SysRelationAuth();
            sysRelationAuth.setLoginType(LoadUserService.Backend.class.getSimpleName());
            sysRelationAuth.setType("role");
            sysRelationAuth.setRelationId(typeId);
            sysRelationAuth.setAuthGuid(guid);
            sysRelationAuthMapper.insertSelective(sysRelationAuth);
        }

    }

    @Override
    public BaseRes<String> delRole(SysRoleVo sysRoleVo) {

        // 判断用户是否有绑定该角色
        int count = sysRoleMapper.haveRelation(sysRoleVo.getId());

        if (count > 0) {
            new BaseRes<>(ServiceCode.DELETE_ROLE_RELATION_FAIL);
        }

        sysRoleMapper.deleteByPrimaryKey(sysRoleVo.getId());


        // 删除角色关联权限
        SysRelationAuthCriteria sysRelationAuthCriteria = new SysRelationAuthCriteria();
        sysRelationAuthCriteria.createCriteria().andLoginTypeEqualTo(LoadUserService.Backend.class.getSimpleName()).andTypeEqualTo("role").andRelationIdEqualTo(sysRoleVo.getId());
        sysRelationAuthMapper.deleteByExample(sysRelationAuthCriteria);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<SysRoleVo> roleDetail(SysRoleVo sysRoleVo) {

        BaseRes<SysRoleVo> res = new BaseRes<>(ServiceCode.SUCCESS);

        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(sysRoleVo.getId());

        if (null != sysRole) {
            SysRoleVo roleVo = BeanUtil.voConvert(sysRole, SysRoleVo.class);
            List<String> guids = sysAuthMapper.selectGuid(roleVo.getId(), "role");
            roleVo.setAuths(guids);
            res.setData(roleVo);
        }

        return res;
    }
}
